<template>
	<view class="main">
		<view class="top">
			<view class="raid" @click="GoswTeam">
				<view class="raid_title">
					{{teamName}}
				</view>

				<view class="">
					<u-icon size="40" color="#E8E8E8" name="arrow-right"></u-icon>
				</view>
			</view>
			<search-top style="padding: 20rpx 0;background-color: white;" placeholder="请输入关键词" @mySearch="mySearch"></search-top>
			<scroll-view scroll-x="true" class="">
				<view class="nav_box">
					<view class="green_button" v-for="n,key in navList" :key="n.name"
						:class="{'un_active' : n.unActive}" @click="checkNav(n,key)">
						{{n.name}}
					</view>
				</view>
			</scroll-view>
		</view>

		<view class="tips">
			<view class="left">
				全部（{{total}}）
			</view>
			<view class="right" @click="upUserFilter" v-if="!noTeam">
				<view class="">
					全部执行人
				</view>
				<view class="">
					<u-icon size="32" color="#ccc" name="arrow-down"></u-icon>
				</view>
			</view>
		</view>

		<view class="card_box" v-if="list.length>0">
			<view class="card_content" v-for="n in list" @click="goDetails(n.planId)">
				<view class="card_title">
					<view class="title_left">
						{{n.planName}}
					</view>
					<view class="icon_right" :class="{
					'in_progress':state(n.planStatus) == '进行中',
					'done':state(n.planStatus) == '已完成',
					'close':state(n.planStatus) == '已关闭'
					}">
						{{state(n.planStatus)}}
					</view>
				</view>
				<view class="sub_title_card">
					执行成员：{{n.planUserName}}
				</view>
				<view class="sub_title_card">
					目标患者：{{n.targetCustomerNum}}人
				</view>
				<view class="sub_title_card" style="padding-bottom: 24rpx;color: #999999;">
					{{n.createTime}}由 {{n.createUser}} 创建
				</view>
			</view>
		</view>

		<view class="job_empty" v-if="empty">
			<view class="">
				<u-image width="335rpx" height="320rpx" :src="actionImg"></u-image>
			</view>
			<view class="empty_text">
				{{emptyText}}
			</view>
		</view>

		<u-popup v-model="showUser" mode="top" border-radius="40" :safe-area-inset-bottom="true"
			:mask-close-able="false">
			<huanzhe-team-list @close="Userconfirm" @confirm="Userconfirm" :mytype="form.typeCodeValue"
				ref="teamChild"></huanzhe-team-list>
		</u-popup>
		<bottom-button text="新建随访计划" @confirm="confirmBtn"></bottom-button>
		<go-nav></go-nav>
	</view>
</template>

<script>
	import {
		suifangList
	} from '@/api/index.js'
	import {
		chooseTeamNow
	} from '@/utils/index.js'
	import pageMixinNorRequest from '@/utils/pageMixinNorRequest.js'
	export default {
		mixins: [pageMixinNorRequest],
		data() {
			return {
				indexKey: null, //点击的上次下标
				showUser: false,
				form: {
					typeCodeValue: 2
				},
				navList: [{
						name: '全部',
						unActive: false,
						state: ''
					},
					{
						name: '待开始',
						unActive: true,
						state: 0
					},
					{
						name: '进行中',
						unActive: true,
						state: 1
					},
					{
						name: '已关闭',
						unActive: true,
						state: -1
					},
					{
						name: '已完成',
						unActive: true,
						state: 2
					},
				],
				actionImg: require('@/static/img/No_follow.png'),
				emptyText: '暂无随访计划',
				QueryParams: {
					teamIdOrUserId: '',
					planStatus: '',
					name: '',
					planUserList: ''
				},
				total: 0,
				teamName: '',
				noTeam: false,
				//emptyJob:true,

			}
		},
		async onShow() {
			await this.clearPageStatus()
			await this.isHasTeam()
			await this.clearVuex()
			this.getTeam()
		},
		watch: {
			'QueryParams.planStatus'(ne) {
				console.log(123);
				this.clearPageStatus()
				this.getTeam()
			},

		},
		computed: {
			state(num) {
				return (num) => {
					switch (num) {
						case 0:
							return '待开始'
						case 1:
							return '进行中'
						case 2:
							return '已完成'
						case -1:
							return '已关闭'
						case -2:
							return '已过期'
						default:
							return '待开始'
					}
				}
			}
		},
		methods: {
			async isHasTeam() {
				await chooseTeamNow()
				let res = uni.getStorageSync('myTeam')
				if (res && res?.code == 200) {
					this.teamName = res.data.name
					this.QueryParams.teamIdOrUserId = uni.getStorageSync('myTeam').data.id
				} else {
					this.noTeam = true
				}
			},
			GoswTeam() {
				uni.navigateTo({
					url: '/pages/team/switchTeam'
				})
			},
			async getMyList(obj) { //请求,这个最好在使用那个组件中使用，这个地方可以扩展接参数
				//例如， {..obj,id:15}
				let res = await suifangList({
					...obj,
					...this.QueryParams
				})
				this.total = res.total
				//console.log(obj);
				//this.teamNumber = res.total //单独需要赋值的地方
				return res
			},
			// async getList(){
			// 	let res = await suifangList()
			// 	console.log(res);
			// },
			goDetails(planId) {
				uni.navigateTo({
					url: `/pages/follow/details?planId=${planId}`
				})
			},
			clearVuex() {
				this.$store.commit('PICKTemplate', '') //清空选择模板
				this.$store.commit('SUIFANGuserSelected', '')
				this.$store.commit('SUIFANGhuanzheSelected', '')
				this.$store.commit('JOBTYPEShow', '定时任务')
				this.$store.commit('JobFORMClear', '')
				this.$store.commit('SHOWJOBLISTALL', [])
			},
			Userconfirm(list) {
				if (list.length > 0) {
					let arr = list.reduce((pre, val, index) => {
						if (val.check) {
							pre.push(val.userId)
						}
						return pre
					}, [])
					if (arr.length > 0) {
						this.QueryParams.planUserList = arr
					} else {
						this.QueryParams.planUserList = '' //什么都不选就清空上一次选的人
					}
					this.clearPageStatus()
					this.getTeam()
				}
				this.showUser = false
			},
			confirmBtn() {
				uni.navigateTo({
					url: '/pages/follow/newproject'
				})
			},
			mySearch(val) {
				this.QueryParams.name = val
				this.clearPageStatus()
				this.getTeam()
			},
			checkNav(obj, key) {
				this.navList.forEach(v => v.unActive = true)
				this.navList[key].unActive = false //选中目标绿色
				if (this.indexKey !== key) { //每次点击，激活的就不会被再次请求
					this.indexKey = key
					//this.$emit('navSel', obj)
					this.QueryParams.planStatus = obj.state
					//console.log(obj);
				}
			},
			upUserFilter() {
				this.showUser = true
				// if(!this.firstInUserFlag){
				// 	this.$refs.teamChild.$emit('startRequest')  //只有点击过滤标签列表，拉起页面才去请求接口
				// 	this.firstInUserFlag = true
				// }
				this.$nextTick(() => {
					this.$refs.teamChild.clearPageStatus()
					this.$refs.teamChild.getTeam()
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	.main {
		background-color: #F8F8F8;

		// position: absolute;
		// width: 100%;
		// min-height: 100vh;
		.top {
			background-color: white;

			.raid {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 42rpx 24rpx 20rpx 24rpx;
				border: 1px solid #e8e8e8;

				.raid_title {
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 32rpx;
					color: #212121;
					line-height: 1.5;
				}
			}

			.nav_box {
				font-family: PingFang SC, PingFang SC;
				font-size: 30rpx;
				padding-bottom: 22rpx;
				//position: fixed;
				//width: 100%;
				display: flex;
				background-color: #FFFFFF;
				justify-content: space-around;
				padding: 0 14rpx 22rpx 14rpx;
				margin-left: 100rpx;

				.green_button {
					border-radius: 20rpx;
					//padding: 16rpx 20rpx;
					background: #4DC0D9;
					color: #FFFFFF;
					width: 140rpx;
					padding: 16rpx 0;
					text-align: center;
					flex-shrink: 0;
					margin-right: 22rpx;
				}



				.un_active {
					border-radius: 20rpx;
					font-weight: 500;
					//padding: 16rpx 20rpx;
					background: #F2F2F2;
					color: #333333;
				}
			}
		}

		.tips {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 24rpx;
			font-family: PingFang SC, PingFang SC;
			font-size: 30rpx;
			color: #666666;
			line-height: 1.5;

			.left {}

			.right {
				color: #333333;
				display: flex;
				align-items: center;
			}
		}

		.card_box {
			background-color: #f8f8f8;
			padding-bottom: 250rpx;
			min-height: 100vh;

			.card_content {
				background-color: white;
				border-radius: 20rpx;
				//margin-bottom: 20rpx;
				margin: 0 20rpx 20rpx 20rpx;

				.card_title {
					padding: 0 24rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					font-family: PingFang SC, PingFang SC;
					font-weight: bold;
					font-size: 34rpx;
					color: #212121;
					line-height: 40rpx;
					padding-top: 24rpx;
					padding-bottom: 20rpx;

					.title_left {}

					.icon_right {
						font-family: PingFang SC, PingFang SC;
						font-weight: bold;
						font-size: 26rpx;
						color: #333333;
						line-height: 30rpx;
						text-align: center;
						padding: 6rpx 20rpx;
						background-color: #F2F2F2;
						border-radius: 200rpx;
					}

					.in_progress {
						background-color: #E5F4FF;
						color: #189AFF;
					}

					.done {
						background-color: #E2FFF2;
						color: #36B278;
					}

					.close {
						background-color: #F1F2F4;
						color: #999999;
					}
				}

				.sub_title_card {
					padding: 0 24rpx;
					font-family: PingFang SC, PingFang SC;
					font-size: 32rpx;
					color: #666666;
					line-height: 1.5;

				}
			}

		}

		.job_empty {
			padding-top: 126rpx;
			min-height: 100vh;
			padding-bottom: 210rpx;
			display: flex;
			flex-direction: column;
			//justify-content: center;
			align-items: center;

			.empty_text {
				font-weight: 500;
				font-size: 36rpx;
				color: #C7C7C7;
				line-height: 1.5;
				text-align: center;
			}
		}
	}
</style>